package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Override
    public PageDTO<Item> pageAll(Integer page, Integer size) {

        IPage p = new Page(page,size);
        page(p);
        List records = p.getRecords();
        long total = p.getTotal();
        return  new PageDTO<>(total,records);
    }

    @Override
    public void stock(Long itemId, Integer num) {
        Item item = getById(itemId);
        if (item.getStock()>=num) {
            item.setStock(item.getStock() - num);
            updateById(item);
        }
        else {
            System.out.println("支付失败");
        }
    }
}
